//
//  AuthCodeLabel.m
//  FuncFanc
//AuthCodeLabelAuthCodeLabelAuthCodeLabelAuthCodeLabel
//  Created by careate-hubing on 2020/11/11.
//  Copyright © 2020 huhu. All rights reserved.
//

#import "AuthCodeLabel.h"

@interface AuthCodeLabel ()

@end

@implementation AuthCodeLabel

-(instancetype)init
{
    if (self = [super init]) {
//        [self setRandomAttributedText];
//        self.userInteractionEnabled = YES;
        //卡住了，点击刷新事件，没有反应。bug
        [self addTarget:self action:@selector(setRandomAttributedText) forControlEvents:UIControlEventTouchUpInside];
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setRandomAttributedText)];
//        [self addGestureRecognizer:tap];
    }
    return self;
}

-(void)refresh
{
    [self setRandomAttributedText];
}

-(void)setRandomAttributedText
{
//    self.currentCode = [self setRandomText];
//    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:self.currentCode];
//    [attStr addAttributes:@{NSFontAttributeName:[self setRandomFont],NSForegroundColorAttributeName:[self setRandomColor]} range:NSMakeRange(0, 1)];
//    [attStr addAttributes:@{NSFontAttributeName:[self setRandomFont],NSForegroundColorAttributeName:[self setRandomColor]} range:NSMakeRange(1, 1)];
//    [attStr addAttributes:@{NSFontAttributeName:[self setRandomFont],NSForegroundColorAttributeName:[self setRandomColor]} range:NSMakeRange(2, 1)];
//    [attStr addAttributes:@{NSFontAttributeName:[self setRandomFont],NSForegroundColorAttributeName:[self setRandomColor]} range:NSMakeRange(3, 1)];
//    [self setAttributedTitle:attStr forState:UIControlStateNormal];
}

// 生成四位，数字，大小写字母组成的 验证码。

// 生成随机背景色，生成与背景色相配的随机字体颜色。

// 生成 13- 17 随机字体大小。
-(NSString *)setRandomText
{
    NSString *code = @"";
    NSArray *arabicNumerals = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    // 0-9  // A-Z   a-z
    NSArray *capEnglishAlphabet = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    NSArray *lowerCaseEnglishAlphabet = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z"];
    NSArray *source = @[arabicNumerals,capEnglishAlphabet,lowerCaseEnglishAlphabet];
    
    for (int i = 0 ; i < 4; i++) {
        int x = arc4random() % source.count;
        NSArray *tempArr = source[x];
        int y = arc4random() % tempArr.count;
        NSString *newLetter = tempArr[y];
        code = [NSString stringWithFormat:@"%@%@",code,newLetter];
    }
    return code;
}

-(UIFont *)setRandomFont
{
    NSArray *fonts = @[@(32.0),@(34.0),@(36.0),@(38.0),@(40.0)];
    int x = arc4random() % fonts.count;
    CGFloat num = [fonts[x] floatValue];
    UIFont *font = [UIFont boldSystemFontOfSize:num];
    return font;
}

-(UIColor *)setRandomColor
{
    CGFloat red = (arc4random() % 245) + 11;
    CGFloat green = (arc4random() % 245) + 11;
    CGFloat blue = (arc4random() % 235) + 21;
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:0.9];
    return color;
}


@end
